/*
    This file is part of Darwin.

    Darwin is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
    Darwin uses GALib
    Copyright (c) 1995-1996 Massachusetts Institute of Technology (MIT)
    Copyright (c) 1996-2005 Matthew Wall
    All rights reserved.
    
    Darwin uses ConfigFile
    Copyright (c) 2004 Richard J. Wagner
     
    Darwin, a Genetic Algorithm to solve Timetabling problem
    Copyright (c) 2006 Christian S. Perone
*/  

#include "tablemapping.h"

sql_create_4(ProfessorDisciplinaTable, 1, 0,
     int, codprofessordisciplina,
     int, codprofessor,
     int, coddisciplina,
     int, preferencial)
     
sql_create_3(OferecimentoTable, 2, 0,
     int, coddisciplina,
     int, nivel,
     int, turno)
     
sql_create_5(DisciplinaTable, 1, 0,
     int, coddisciplina,
     int, creditos,
     int, laboratorio,
     int, podecairemferiado,
     int, teoricopratico)
     
sql_create_2(CursoTable, 1, 0,
     int, codcurso,
     int, laboratorios)

sql_create_3(ProfessorTable, 1, 0,
     int, codprofessor,
     int, horas,
     int, professordaarea)
     
sql_create_3(DisponibilidadeTable, 3, 0,
     int, codprofessor,
     int, codtimeslot,
     int, coddiasemana)
     
sql_create_2(DiaSemanaTable, 1, 0,
     int, coddiasemana,
     int, feriados)
     
////////////////////////////////////////////////////////////////////////

void MapProfessorDisciplina::LoadData(void)
{
     mysqlpp::Query query_profdis = g_connection.query();
     
     query_profdis << "SELECT	  codprofessordisciplina, \
                                codprofessor, \
                                coddisciplina,\
                                preferencial \
                       FROM     PROFESSORDISCIPLINA \
                       WHERE    AnoSemestre = " << g_options.timetabling_anosemestre << \
                     " AND      CodCurso = " << g_options.timetabling_curso;
                       
     query_profdis.storein(mInsideVector);
     query_profdis.reset();
}

MapProfessorDisciplina::t_iterator MapProfessorDisciplina::GetInfo(int codprofessordisciplina)
{
     std::vector<ProfessorDisciplinaTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->codprofessordisciplina==codprofessordisciplina)
               return it;
     return mInsideVector.end();
}

////////////////////////////////////////////////////////////////////////

void MapOferecimento::LoadData(void)
{
     mysqlpp::Query query_oferecimento = g_connection.query();
     
     query_oferecimento << "SELECT	coddisciplina, \
                                        nivel, \
                                        turno \
                            FROM	OFERECIMENTO \
                            WHERE	AnoSemestre = " << g_options.timetabling_anosemestre << \
                          " AND         CodCurso = " << g_options.timetabling_curso;
                       
     query_oferecimento.storein(mInsideVector);
     query_oferecimento.reset();
}

MapOferecimento::t_iterator MapOferecimento::GetInfo(int coddisciplina, int nivel)
{
     std::vector<OferecimentoTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(
               (it->coddisciplina==coddisciplina) &&
               (it->nivel==nivel)
            )
               return it;

     return mInsideVector.end();
}
////////////////////////////////////////////////////////////////////////

void MapDisciplina::LoadData(void)
{
     mysqlpp::Query query_disciplina = g_connection.query();
     
     query_disciplina << "SELECT	coddisciplina, \
                                   creditos, \
                                   laboratorio, \
                                   podecairemferiado, \
                                   teoricopratico \
                            FROM	DISCIPLINA";
                       
     query_disciplina.storein(mInsideVector);
     query_disciplina.reset();
}

MapDisciplina::t_iterator MapDisciplina::GetInfo(int coddisciplina)
{
     std::vector<DisciplinaTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->coddisciplina==coddisciplina) return it;
     return mInsideVector.end();
}
/////////////////////////////////////////////////////////////////////////

void MapCurso::LoadData(void)
{
     mysqlpp::Query query_curso = g_connection.query();
     
     query_curso << "SELECT CodCurso, \
                            Laboratorios \
                    FROM    CURSO";

     query_curso.storein(mInsideVector);
     query_curso.reset();
}

MapCurso::t_iterator MapCurso::GetInfo(int codcurso)
{
     std::vector<CursoTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->codcurso==codcurso) return it;
     return mInsideVector.end();
}

/////////////////////////////////////////////////////////////////////////////

void MapProfessor::LoadData(void)
{
     mysqlpp::Query query_professor = g_connection.query();
     
     query_professor << "SELECT CodProfessor, \
                            Horas, \
                            ProfessorDaArea \
                    FROM    PROFESSOR";

     query_professor.storein(mInsideVector);
     query_professor.reset();
}

MapProfessor::t_iterator MapProfessor::GetInfo(int codprofessor)
{
     std::vector<ProfessorTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->codprofessor==codprofessor) return it;
     return mInsideVector.end();
}

/////////////////////////////////////////////////////////////////////////////

void MapDisponibilidade::LoadData(void)
{
     mysqlpp::Query query_disponibilidade = g_connection.query();
     
     query_disponibilidade << "SELECT CodProfessor, \
                            CodTimeSlot, \
                            CodDiaSemana \
                    FROM    DISPONIBILIDADE \
                    WHERE   AnoSemestre = " << g_options.timetabling_anosemestre << \
                  " AND     CodCurso = " << g_options.timetabling_curso;

     query_disponibilidade.storein(mInsideVector);
     query_disponibilidade.reset();
}

MapDisponibilidade::t_iterator MapDisponibilidade::GetInfo(int codprofessor)
{
     std::vector<DisponibilidadeTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->codprofessor==codprofessor) return it;
     return mInsideVector.end();
}

/////////////////////////////////////////////////////////////////////////////

void MapDiaSemana::LoadData(void)
{
     mysqlpp::Query query_diasemana = g_connection.query();
     
     query_diasemana << "SELECT CodDiaSemana, \
                            Feriados \
                    FROM    DIASEMANA \
                    WHERE   AnoSemestre = " << g_options.timetabling_anosemestre;

     query_diasemana.storein(mInsideVector);
     query_diasemana.reset();
}

MapDiaSemana::t_iterator MapDiaSemana::GetInfo(int coddiasemana)
{
     std::vector<DiaSemanaTable>::iterator it;
     for(it=mInsideVector.begin(); it!=mInsideVector.end(); it++)
          if(it->coddiasemana==coddiasemana) return it;
     return mInsideVector.end();
}

int MapDiaSemana::GetFeriados(int codtimeslot)
{
     t_iterator it;
     int factor=0;
     
     if( (codtimeslot>=turnos::TARDE_BEGIN) && (codtimeslot<=turnos::TARDE_END) )
          factor=5;
          
     if( (codtimeslot>=turnos::NOITE_BEGIN) && (codtimeslot<=turnos::NOITE_END) )
          factor=10;

     it = GetInfo((int) (codtimeslot/2)-factor );
     assert(it!=GetEnd());
     return it->feriados;
}
